import time
import random
import requests
from lxml import etree

from auto_range_phone import Bequtiful_Phone
from get_user_agent import User_Agent_Help
from mysqlphonenumber import MysqlHelp
ua=User_Agent_Help()
my=MysqlHelp()

class Phone_12349(Bequtiful_Phone):
    

    #初始化对象，为对象添加url和headers属性
    def __init__(self):

        self.url='http://www.1234i.com/p.php'
        self.headers={
                'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
                'Accept-Encoding':'gzip, deflate',
                'Accept-Language':'zh-CN,zh;q=0.9',
                'Cache-Control':'max-age=0',
                'Content-Length':'186',
                'Content-Type':'application/x-www-form-urlencoded',
                'Host':'www.1234i.com',
                'Origin':'http://www.1234i.com',
                'Referer':'http://www.1234i.com/p.php',
                'Upgrade-Insecure-Requests':'1',
                'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',

        }

        
    # 准备数据，为验证是否是哪个地区的电话号段做准备
    def prepare_data(self):
        phone_list=[]
        second_part=[str(i)+str(j)+str(m)+str(n) for i in range(10) for j in range(10) for m in range(10) for n in range(10)]
        for j in second_part:
            half_phone='153'+j
            phone_list.append(half_phone)
        return phone_list
                


    # 根据准备的数据，为验证的号段的网址，发送请求
    def send_request(self,url,data):
       
        form={
            'haomas':'\r\n'.join(data),
            'ok3':'20200103-15..28.00-113.225.157.141',
            'Submit':'(unable to decode value)',
        }        
        try:
            print(url)
            response=requests.post(url,headers=self.headers,data=form)
            response.encoding='gb2312'
            print(self.headers['User-Agent'])
            print(response.status_code)
            return response.content
        except:
            self.headers['User-Agent']=ua.get_useragent()
            time.sleep(240)
            response=self.send_request(url,data)
            
        


    # 过滤数据，把想要的数据song网页内容中取出
    def filter_data(self,data):
        # print(data)
        html_ele=etree.HTML(data)
        half_part=html_ele.xpath('//p//u/text()')
        info=html_ele.xpath('//p//font/text()')
        provinces=[item.split(',')[0].strip() for item in info]
        card_type=[item.split(',')[-1].strip() for item in info]
        phone=list(zip(half_part,provinces,card_type))
        phone=[item for item in phone if item[1]!='']
        if phone:
            return phone
        else:
            return False



    # 保存数据，加你个想要的数据存储到指定文件里
    # 一个城市的写入到一起，表名叫省加城市名
    # def save_data(self,data):
    #     sql='select * from phone_number where number_segment={}'
    #     if data and (not my.execute_sql(sql=sql.format(data[0]),mode='r')):
    #         sql='insert into phone_number(number_segment,provinces,city,card_type) values(%s,%s,%s,%s)'
    #         if my.execute_sql(sql,data,mode='w'):
    #             print('存储成功')
        
        





        # # 创建一个新的工作簿
        # book=xlwt.Workbook(encoding='utf-8',style_compression=0)
        # # 在工作簿里创建工作表
        # sheet=book.add_sheet('shenyang',cell_overwrite_ok=True)
        # # 行列值
        # for i in range(len(phone_list)):
        #     sheet.write(i,0,phone_list[i])
            
        # # 保存工作簿到指定路径里
        # book.save('./phone.xls')

    def save_data(self,data):
        print(data)
        half_part=tuple([item[0] for item in data])
        arg_list = ','.join(['%s'] * len(data))
        sql=f'select * from phone_number where number_segment in ({arg_list})'
        if not my.execute_sql(sql=sql,data=half_part,mode='r'):
            sql='insert into phone_number(number_segment,provinces,card_type) values(%s,%s,%s)'
            if my.execute_sql(sql,data):
                print('存储成功')
            else:
                print('存储失败')
        else:
            print('数据已存在')
        

    # 运行函数，所有的函数都在此处被调用
    def run(self):
        phone_list=[]
        phone_list=self.prepare_data()
        i=0   #i*100-i*100+100
        while i<=100:
            time.sleep(random.randint(6,11))
            self.headers['User-Agent']=ua.get_useragent()
            data=phone_list[i*100:i*100+100]
            data=self.send_request(self.url,data)
            phone=self.filter_data(data)
            if phone:
                self.save_data(phone)
            else:
                print('查询的号段为空')
            i+=1

           
            



    # def process_speed(self):
    #     p=Pool(3)
    #     p.apply_async(func=self.run)
    #     p.close()
    #     p.join()

    

# 程序的入口
if __name__ == '__main__':
    bphone=Phone_12349()
    bphone.run()

